package com.unissoft.ordersystem.dataaccess.common;

/**
 * 客户端分页条件与服务器端分页条件转换
 * 
 * @author aaron
 * 
 */
public class PageUtill {
	public static void convertPagination(Pagination paginationPo,
			PageView paginationBo) {
		int paginationBoPageSize = paginationBo.getPageSize();
		int paginationBoPageIndex = paginationBo.getPageIndex();
		int totalRecord = paginationBo.getTotalRecord();
		int firstIndex = 1;
		int index;
		int startRow;

		// paginationBoPageSize 不能小于 0
		if (paginationBoPageSize < 0) {
			paginationBoPageSize = Math.abs(paginationBoPageSize);
		}
		// paginationBoPageIndex 不能小于 1
		if (paginationBoPageIndex < 1) {
			paginationBoPageIndex = firstIndex;
		}
		// paginationBoPageIndex 不能超过 index
		index = (int) Math.ceil((float) totalRecord / paginationBoPageSize);
		if (paginationBoPageIndex > index && 0 != index) {
			paginationBoPageIndex = index;
		}

		// 获取 startRow
		startRow = (paginationBoPageIndex - 1) * paginationBoPageSize;

		// 获取 Po
		paginationPo.setStartRow(startRow);
		paginationPo.setPageSize(paginationBoPageSize);
	}
}
